﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Framework.Common;
using Dal.Entities;

namespace Web.Controllers
{
    [ValidateInput(false)]
    public class MsgController : Controller
    {
        //
        // GET: /Msg/

        public ActionResult Index()
        {
            int pageindex = TypeConvertHelper.StrToInt(Request["pageindex"], 1);
            int recordcount;
            IList<Message> List = null;
            Framework.Data.IRepository rep = Framework.Data.RepositoryFactory.GetRepository();
            if (Common.Admin())
            {
                List = rep.GetPaged<Message, DateTime>(pageindex - 1, 10, out recordcount, i => i.FlagDelete == false, i => i.AddTime, true);
            }
            else
            {
                int userid = Common.GetUserID();
                List = rep.GetPaged<Message, DateTime>(pageindex - 1, 10, out recordcount, i => i.SenderUserID == userid && i.FlagDelete == false, i => i.AddTime, true);
            }
            ViewBag.RecordCount = recordcount;
            ViewBag.PageIndex = pageindex;
            return View(List);
        }


        public ActionResult Add()
        {
            if (Request.HttpMethod.ToLower() == "get")
            {
                return View();
            }
            else if (Request.HttpMethod.ToLower() == "post")
            {
                Framework.Data.IRepository rep = Framework.Data.RepositoryFactory.GetRepository();

                Message msg = new Message();
                UpdateModel(msg);
                msg.FlagDelete = false;
                msg.AddTime = DateTime.Now;
                msg.SenderUserID = Common.GetUserID();
                rep.AddThenSaveChange(msg);
                Common.AlertRedirect("操作成功", "/msg");
                return View(msg);
            }
            return null;
        }
        [MyAuthAttribute(Role = 1)]
        public ActionResult Reply()
        {
            Framework.Data.IRepository rep = Framework.Data.RepositoryFactory.GetRepository();
            int msgid = TypeConvertHelper.StrToInt(Request["id"], 1);
            var msg = rep.GetByKey<Message>(msgid);
            if (msg != null)
            {
                if (Request.HttpMethod.ToLower() == "get")
                {
                    return View(msg);
                }
                else if (Request.HttpMethod.ToLower() == "post")
                {
                    rep.Attach(msg);
                    UpdateModel(msg);
                    msg.ReplyTime = DateTime.Now;
                    rep.Save();
                    Common.AlertRedirect("操作成功", "/msg");
                    return View(msg);
                }
            }
            return null;
        }

        public ActionResult Delete()
        {
            int msgid = TypeConvertHelper.StrToInt(Request["id"], 1);
            Framework.Data.IRepository rep = Framework.Data.RepositoryFactory.GetRepository();
            var msg = rep.GetByKey<Message>(msgid);
            if (msg != null)
            {
                if (Common.Admin() || msg.SenderUserID == Common.GetUserID())
                {
                    rep.AttachThenSaveChange(msg, i => i.FlagDelete = true);
                    rep.Save();
                    Common.AlertRedirect("操作成功", "/msg");
                }
            }
            return null;
        }

    }
}
